<?php



define('UpLoadKey', 'game^tsomh@tank');
if($_POST['token']==UpLoadKey){
	include_once 'db.php';
	mysql_query('set @ip="'.$_POST['ip'].'"', $c);
	mysql_query('set @port='.$_POST['port'], $c);
	mysql_query('set @num='.$_POST['num'], $c);
	mysql_query('set @check='.$_POST['check'], $c);
	mysql_query('delete from '.TB_GameSvr.' where ip=@ip and port=@port limit 1', $c);
	mysql_query('insert into '.TB_GameSvr.' values (@ip,@port,@num,@check, NOW())', $c);
}

if(array_key_exists('getSvr',$_REQUEST)){
	include_once 'db.php';
	$now = time();
	$rst = mysql_query('select ip,port,checkT,UNIX_TIMESTAMP(updateTime) as dt from '.TB_GameSvr.' order by num', $c);
	while($row = mysql_fetch_assoc($rst)){
		if($now - (int)$row['dt'] > (int)$row['checkT']){
			mysql_query('delete from '.TB_GameSvr.' where ip="'.$row['ip'].'" and port='.$row['port'].' limit 1', $c);
		}
		else{
			echo json_encode(array('ip'=>$row['ip'], 'port'=>$row['port']));
			return;
		}
	}
	echo json_encode(array('err'=>1));;
}

